﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class frmPhieutra : DevExpress.XtraEditors.XtraForm
    {
        DAL.Entities_DAL.PHIEU_MUON_TRA_DAL _pmt = new DAL.Entities_DAL.PHIEU_MUON_TRA_DAL();
        DAL.Entities_DAL.CTPMT_DAL _ct = new DAL.Entities_DAL.CTPMT_DAL();
        DAL.Entities_DAL.QLTB_DAL _qltb = new DAL.Entities_DAL.QLTB_DAL();
        DAL.Entities_DAL.THIETBI_DAL _tb = new DAL.Entities_DAL.THIETBI_DAL();
        BLL.Entities_BLL.PMT_BLL _bll = new BLL.Entities_BLL.PMT_BLL();

        public frmPhieutra()
        {
            InitializeComponent();
        }

        private void frmPhieutra_Load(object sender, EventArgs e)
        {
            DataTable _dt = _pmt.load_sttpmt("PHIEU_MUON");
            txtSTTPM.Properties.DataSource = _dt;
        }

        private void txtSTTPM_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                string _is = txtSTTPM.Text;
                string _loai = "PHIEU_MUON";
                _AA = _pmt.load_ctpmt(_is, _loai);

                DataTable _data = _pmt.load_pmt(_is);
                dsPM.DataSource = _AA;

                dtMgaymuon.DataBindings.Clear();
                txtMS.DataBindings.Clear();

                dtMgaymuon.DataBindings.Add("EditValue", _data, ".NGAY_LAP");
                txtMS.DataBindings.Add("EditValue", _data, ".MSSV");
            }
            catch { };
        }

        private void cmdTao_Click(object sender, EventArgs e)
        {
            cbPt.Text = txtSTTPM.Text;
            txtDTuong.Text = txtMS.Text;
        }

        private void cmdTaophieu_Click(object sender, EventArgs e)
        {
            try
            {
                var o = new DAL.Entities.PHIEU_MUON_TRA
                {
                    MA_PMT = cbPt.Text,
                    NGAY_LAP = Convert.ToDateTime(dtNgaytra.EditValue + ""),
                    LOAI_PHIEU = txtPT.Text,
                    MA_NV = "NV01",
                    MSSV = txtDTuong.Text

                };

                //them
                var oki = _pmt.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            catch { MessageBox.Show("Load dữ liệu không đúng!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); };
        }
        DataTable _AA;
    
        private void cmdNhan_Click(object sender, EventArgs e)
        {
            try
            {
                string _loai1 = "PHIEU_MUON";
                string so = txtSTTPM.Text;
                var ngay = Convert.ToDateTime(dtNgaytra.EditValue + "");
                //cap nhap phieu muon thanh phieu tra
                var kt = _pmt.cap_nhat_pt(so, ngay);
                //xoa phieu muon sao khi tra xong
                var _XOA = _pmt.Delete(so, _loai1);

                if (kt && _XOA)
                {
                    txtSTTPM_EditValueChanged(sender, e);
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    string _is = txtSTTPM.Text;
                    string _loai = "PHIEU_TRA";
                    //load lai danh sach phieu tra
                    _AA = _pmt.load_ctpmt(_is, _loai);

                    dsPM1.DataSource = _AA;
                    frmPhieutra_Load(sender, e);

                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            catch
            { MessageBox.Show("Load dữ liệu không đúng!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); };
        }

        private void cmdIn_Click(object sender, EventArgs e)
        {
            string _ma = cbPt.Text;
            var ngay = Convert.ToDateTime(dtMgaymuon.EditValue + "");
            var loai = "PHIEU_TRA";
            var frm = new Report.Report_Base();
            var xr = new Reports.XR_PMT();
            xr.DataSource = _pmt.dsPMT(_ma, ngay, loai);
            frm.SetReport(xr);
            frm.Show();
        }
    }
}